<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="searchForm" :inline="true">
        <el-form-item label="文章名称">
					<el-input v-model="searchForm.articleName" placeholder="文章名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
			style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" >
			</el-table-column>
			<el-table-column type="index" width="70" label="序号">
			</el-table-column>
			<el-table-column prop="articleName" label="文章名称" align="center">
			</el-table-column>
			<el-table-column prop="articleTag" label="文章标签" align="center">
			</el-table-column>
			<el-table-column prop="articlePic" label="文章封面" align="center">
        <template scope="scope">
        <el-image :src="scope.row.articlePic"></el-image>
        </template>
			</el-table-column>
			<el-table-column prop="articleTypeEmp.typeName" label="文章类型" align="center">
			</el-table-column>
			<el-table-column  label="文章状态" align="center">
        <template scope="scope">
          <span v-if="scope.row.articleState===0" >草稿</span>
          <span v-else-if="scope.row.articleState===1" style="color: green">发布</span>
          <span v-else-if="scope.row.articleState===2" style="color: red">弃用</span>
        </template>
			</el-table-column>
      <el-table-column  label="是否置顶" align="center">
        <template scope="scope">
          <span v-if="scope.row.topState" style="color: green">置顶</span>
          <span v-else-if="!scope.row.topState" style="color: red">不置顶</span>
        </template>
			</el-table-column>
      <el-table-column prop="employee.userName" label="发布人" align="center">
			</el-table-column>
      <el-table-column prop="articleReadCount" label="阅读量" align="center">
			</el-table-column>
      <el-table-column prop="articleStarNum" label="点赞数" align="center">
			</el-table-column>
      <el-table-column prop="articleCommentNum" label="评论数" align="center">
			</el-table-column>
      <el-table-column prop="createTime" label="发布时间" align="center">
			</el-table-column>

			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
				:page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="totals" style="float:right;">
			</el-pagination>
		</el-col>
<!--    :rules="saveFormRules" 规则-->
		<!--添加或修改界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false"
               :before-close="handleClose">
			<el-form :model="saveForm" label-width="80px"  ref="saveForm">
				<el-form-item label="文章类型" >
          <el-cascader
              v-model="saveForm.articleType"
              :options="options"
              :props="{checkStrictly: true, value:'id',label:'typeName',emitPath:false}"
              clearable>
          </el-cascader>
				</el-form-item>
        <el-form-item label="文章名称" >
          <el-input v-model="saveForm.articleName"></el-input>
				</el-form-item>
        <el-form-item label="文章标签" >
          <el-input v-model="saveForm.articleTag"></el-input>
        </el-form-item>
				<el-form-item label="文章状态" >
          <el-radio-group v-model="saveForm.articleState">
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">发布</el-radio>
            <el-radio :label="2">弃用</el-radio>
          </el-radio-group>
				</el-form-item>
        <el-form-item label="是否置顶" >
          <el-radio-group v-model="saveForm.topState">
          <el-radio :label="0">不置顶</el-radio>
          <el-radio :label="1">置顶</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文章封面" prop="articlePic" v-model="saveForm.articlePic">
          <el-upload class="upload-demo" action="http://localhost/minio/upload" ref="upload"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :on-change="handleChange"
              :multiple="false"
              name="file"
              :limit="1"
              :file-list="fileList"
              :auto-upload="false"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>


        <el-form-item label="文章简介" prop="articleRemark">
          <el-input type="textarea" v-model="saveForm.articleRemark"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleContent">
          <el-input type="textarea" v-model="saveForm.articleContent"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="handleClose">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

import {format} from "echarts/lib/export";

export default {
	data() {

		return {
      //动态显示数据
      options:[],
      fileList:[], //文件列表
      //查询form
      searchForm: {
        articleName:"", //文章名称
      },

      title:"",
			//1.分页查询相关数据
			totals: 0,//总记录条数
			currentPage: 1,//当前页,要传递到后台的
			pageSize: 10, //每页显示多少条
			tableDatas: [], //当前页数据
			listLoading: false,

			//2.高级查询相关数据
			keyword: '',

			//3.删除相关数据

			//4.批量删除相关数据
			sels: [], //存储多选记录

      changeimage:[], //图片集合
      ischange:"", //设置是否对图片进行改动
			//5.添加和修改相关数据
			saveFormVisible: false,
			saveForm: {
				id: null,
        articleType:"", //文章类型
        articleName:"",  //文章名称
        articleTag:"",   //文章标签
        articleState:"",  //文章状态
        topState:"",   //是否置顶
        articlePic:"",  //文章封面
        articleRemark:"", //文章简介
        articleContent:"", //文章内容

			},



		}
	},
	methods: {
    //把图片加入到集合中
    handleChange(file, fileList){
      this.changeimage.push(file);
    },
    //关闭模态框前清楚图片
    handleClose(){
      this.fileList=[];
      this.saveFormVisible=false;
    },
    //文件上传成功时的钩子
    handleSuccess (response, file, fileList){
      //console.log(response, file, fileList)
      let {success,msg}=response;
      if (success){
        //返回图片地址
        this.saveForm.articlePic=msg;
        //console.log(this.saveForm)
        //手动提交
        this.ManualSubmission();
      }
    },
    //移除图片
    handleRemove(file, fileList) {
      this.changeimage.pop();
      this.ischange=true;
    },


		//1.分页查询相关方法
		//1.1.获取列表数据
		getTableData() {
      //开启加载框
      this.listLoading=true;
      let param={"currentPage":this.currentPage,
                  "pageSize":this.pageSize,
      };
      this.$http.patch("/article",param).then(res=>{

        let{success,data} = res.data;
        if (success){
          //关闭加载
          this.listLoading=false;
          this.tableDatas=data.list
          this.totals=data.total;
        }
      })
			
		},
		//1.2.选择第几页时触发
		handleCurrentChange(curentPage) {
      this.currentPage=curentPage;
      this.getTableData();
		},
		//1.3.选择每页显示记录条数时触发
		handleSizeChange(pageSize) {
			this.currentPage=1;
      this.pageSize=pageSize;
      this.getTableData();
		},

		//2.高级查询相关方法
		keywordQuery() {

      if (this.searchForm.data){
        this.searchForm.starttime=this.searchForm.data[0];
        this.searchForm.endtime=this.searchForm.data[1];
      }else {
        this.searchForm.data=null;
      }
      //console.log(this.searchForm)
      this.currentPage=1;
      this.getTableData();

		},

		//3.删除相关方法
		handleDel(row) {
      //console.log(row)
      this.$confirm('是否确认删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/article/"+row.id).then(res=>{
          //console.log(res)
          let {success}=res.data;
          if (success){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
          this.getTableData();
        })
      });
		},


		//4.批量删除相关方法
		//4.1.选项改变时触发
		handleSelectionChange(val) {
      this.sels=val;
		},
		//4.2.批量删除
		handlePatchDel() {
      this.$confirm('是否确认批量删除?', '批量删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.patch("/article/batchDeletion",this.sels).then(res=>{
          //console.log(res)
          let {success}=res.data;
          if (success){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
          this.getTableData();
        })
      });
		},

		//5.添加和修改相关方法
		//5.1.点击添加弹出对话框
		handleAdd() {
			this.saveFormVisible=true;
      //标题
      this.title="添加文章";
      for (let k in this.saveForm){
        //清空表单
        this.saveForm[k]="";
      }

      //新增默认状态
      this.saveForm["articleState"]=0;
      this.saveForm["topState"]=1;

      //console.log(this.saveForm)
		},

		//5.2.点击修改弹出对话框
		handleEdit(row) {
      //console.log(row.articlePic)
      //标题
      this.title="修改文章";
      this.saveFormVisible=true;
      for (let k in this.saveForm){
          this.saveForm[k]="";
      }
      //清空改动
      this.ischange="";
      //数据回显
      this.saveForm = Object.assign({},row)
      //图片回显
      if (this.saveForm.articlePic){
        this.fileList.push({url:this.saveForm.articlePic});
      }

		},

    //提交表单
    ManualSubmission(){
      this.$http.post("/article",this.saveForm).then(res=>{
        console.log(this.saveForm)
        let {success}=res.data;
        if (success){
          this.saveFormVisible=false;
          //清空图片
          this.fileList=[];
          //清空图片集合
          this.changeimage=[];
          this.getTableData();
        }
      })
    },

		//5.3.提交表单数据操作数据库
		saveSubmit() {
      //判断是新增还是修改，通过id
      if (this.saveForm.id){
        //console.log(this.saveForm.articlePic)
        //判断有没有附件
        if (this.saveForm.articlePic){   //有附件
          //判断图片有没有改动
          if (this.ischange){  //删除了老附件
            let articlePic=this.saveForm.articlePic;
            let index = articlePic.lastIndexOf("/");
            let objectName = articlePic.substring(index+1);
            this.$http.delete("/minio/"+objectName).then(res=>{
              let {success}=res.data
              if (success){
                //有没有添加附件
                if (this.changeimage.length){
                  this.$refs.upload.submit();
                }else { //没有添加新附件
                  //清空图片
                  this.saveForm.articlePic="";
                  this.ManualSubmission();
                }
              }
            })
          }else { //没有改附件
            this.ManualSubmission();
          }
        }else {  //最开始没有附件
          //判断有没有增加附件
          if (this.changeimage.length){
            this.$refs.upload.submit();
          }else {
            //把图片清空
            this.saveForm.articlePic="";
            this.ManualSubmission();
          }
        }

      }else { //没值 新增
        //console.log(this.changeimage.length)
        //有值，代表有附件上传
        if (this.changeimage.length){
          //手动提交
          this.$refs.upload.submit();
        }else {
          //直接提交
          this.ManualSubmission();
        }
      }
    },
    //获取文章树形
    getArticleTypetree(){
      this.$http.patch("/articletype/gettree").then(res=>{
        let {success,data}=res.data;
        // console.log(res)
        if (success){
          this.options=data;

        }
      })
    }

	},
	mounted() {

		this.getTableData();
    this.getArticleTypetree();
	}
}

</script>

<style scoped></style>
